package com.ygqh.baby.controller.kids;

import com.alibaba.fastjson.JSON;
import com.ygqh.baby.ao.ResponseEntity;
import com.ygqh.baby.handler.ProductSolrHandler;
import com.ygqh.baby.kids.entity.bo.KidsProductExt;
import com.ygqh.baby.model.ProductListRequestParam;
import com.ygqh.baby.po.YgSkuMaterial;
import com.ygqh.baby.service.*;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/admin/kids")
@Controller
public class KidsProductController extends BaseKidsController {

    @Autowired
    private YgProductService ygProductService;
    @Autowired
    private YgSkuMaterialService ygSkuMaterialService;
    @Autowired
    private ProductSolrHandler productSolrHandler;
    @Autowired
    private AdminSkuService adminSkuService;
    @Autowired
    private YgAttributeService ygAttributeService;
    @Autowired
    private WxSmallAuthorityService wxSmallAuthorityService;

    @RequestMapping("/loadProductBySkuCode")
    @ResponseBody
    public ResponseEntity loadProductBySkuCode(String[] skuCodes, String[] productCodes, HttpServletRequest request) {
        ResponseEntity validToken = validToken(request);
        if (!validToken.isSuccess()) {
            return validToken;
        }
        logger.info("【loadProductBySkuCode】收到线下店拉取商品请求。skuCodes={}, productCodes={}",
                Arrays.toString(skuCodes), Arrays.toString(productCodes));
        List<KidsProductExt> productList = adminSkuService.findProductDataForKids(skuCodes, productCodes);
        if (CollectionUtils.isEmpty(productList)) {
            return ResponseEntity.error("查询不到相关壹果商品信息，商品编码错误或壹果商品信息不全", null);
        }
        return ResponseEntity.success(productList);
    }

    @RequestMapping("/loadSkuMaterial")
    @ResponseBody
    public ResponseEntity loadSkuMaterial(String skuCode, String barCode, HttpServletRequest request) {
        ResponseEntity validToken = validToken(request);
        if (!validToken.isSuccess()) {
            return validToken;
        }
        YgSkuMaterial skuMaterial = null;
        if (StringUtils.isNotBlank(skuCode)) {
            skuMaterial = ygSkuMaterialService.findBySkuCode(skuCode);
        } else if (StringUtils.isNotBlank(barCode)) {
            skuMaterial = ygSkuMaterialService.findSkuByBarCode(barCode);
        }
        return ResponseEntity.success(skuMaterial);
    }

    /**
     * 拉取solr商品库数据
     *
     * @param requestParam
     * @param request
     * @return
     */
    @RequestMapping("/loadProductBySolr")
    @ResponseBody
    public ResponseEntity loadProductBySolr(ProductListRequestParam requestParam, HttpServletRequest request) {
        ResponseEntity validToken = validToken(request);
        if (!validToken.isSuccess()) {
            return validToken;
        }
        logger.info("【loadProductBySkuCode】收到拉取solr商品库数据请求。requestParam={}", JSON.toJSONString(requestParam));
        return ResponseEntity.success(productSolrHandler.productList(requestParam));
    }

    /**
     * 根据商品编码拉取商品详情
     * @param productCode
     * @param request
     * @return
     */
    @RequestMapping("/loadProductDetailByProductCode")
    @ResponseBody
    public ResponseEntity loadProductDetailByProductCode(String productCode, HttpServletRequest request) {
        ResponseEntity validToken = validToken(request);
        if (!validToken.isSuccess()) {
            return validToken;
        }
        logger.info("【loadProductBySkuCode】收到根据商品编码拉取商品详情请求。productCode={}", productCode);
        List<KidsProductExt> productList = adminSkuService.findProductDataForKids(null, new String[]{productCode});
        if (CollectionUtils.isEmpty(productList)) {
            return ResponseEntity.error("查询不到商品[" + productCode + "]信息", null);
        }
        KidsProductExt detailInfo = productList.get(0);
        Map<String, Object> data = new HashMap<>(2);
        data.put("detail", detailInfo);
        if (detailInfo.getGroup().getYiigooId() != null) {
            List<Map<String, Object>> productByGroup =
                    ygProductService.findProductByGroupId(0L, detailInfo.getGroup().getYiigooId());
            data.put("productByGroup", productByGroup);
        }
        return ResponseEntity.success(data);
    }

    /**
     * 拉取后台商品属性
     *
     * @return
     */
    @RequestMapping("/loadProductAttribute")
    @ResponseBody
    public ResponseEntity loadProductAttribute() {
        return ResponseEntity.success(ygAttributeService.findByParentId(15L));
    }

    /**
     * 拉取小程序码
     *
     * @return
     */
    @RequestMapping("/loadSmallQrCode")
    @ResponseBody
    public ResponseEntity loadSmallQrCode(@RequestParam String url, @RequestParam String platformNo,
                                          HttpServletRequest request) {
        ResponseEntity validToken = validToken(request);
        if (!validToken.isSuccess()) {
            return validToken;
        }
        return ResponseEntity.success(wxSmallAuthorityService.getSamllQrcode(url, 1, platformNo));
    }
}
